package Calculator;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import Calculator.*;
public class calculator extends JFrame{
	private static JTextField jtf=new JTextField(100);
	private JPanel jp1;
	private JPanel jp2;
	private JButton jbt[]=new JButton[22];
        private String s2="";
        private String str1="";
        private int count=0;
        private int SetCount=-1;
        private Nbl a=new Nbl();//逆波兰函数的调用
        private int ShowCount=-1;
       
	public calculator(){
		jtf.setSize(500,35);
		jtf.setLocation(0,25);
                jtf.setText("0");
		JFrame jfm=new JFrame("计算器");
		jfm.setSize(500,340);
		jfm.setLayout(null);
	jp1=new JPanel();
	jp1.setBounds(0,60,500,40);
	jp2=new JPanel();
	jp2.setBounds(0,100,500,200);
	GridLayout gl1=new GridLayout(1,2);
	GridLayout gl2=new GridLayout(5,4);
	jp1.setLayout(gl1);
	jp2.setLayout(gl2);
        for(int i=0;i<=9;i++)
        	jbt[i]=new JButton(""+i);
        JMenu jm1=new JMenu("文件");
        JMenu jm2=new JMenu("帮助");
        JMenuItem ji1=new JMenuItem("退出");
        JMenuItem ji2=new JMenuItem("关于");
        JMenuBar bar=new JMenuBar();
        bar.add(jm1);
        bar.add(jm2);
        jm1.add(ji1);
        jm2.add(ji2);
        jfm.setJMenuBar(bar);
        ji1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
        ji2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null,"2013020100019");
            }
        });
        jbt[10]=new JButton("Back");jbt[11]=new JButton("C");
        jbt[12]=new JButton("/");jbt[13]=new JButton("*");
        jbt[14]=new JButton("-");jbt[15]=new JButton("+");
        jbt[21]=new JButton("=");jbt[17]=new JButton("sqrt");
        jbt[18]=new JButton("%");jbt[19]=new JButton("1/x");
        jbt[20]=new JButton(".");jbt[16]=new JButton("+/-");
        jp1.add(jbt[10]);jp1.add(jbt[11]);jp2.add(jbt[7]);jp2.add(jbt[8]);
        jp2.add(jbt[9]);jp2.add(jbt[12]);jp2.add(jbt[4]);jp2.add(jbt[5]);
        jp2.add(jbt[6]);jp2.add(jbt[13]);jp2.add(jbt[1]);jp2.add(jbt[2]);
        jp2.add(jbt[3]);jp2.add(jbt[14]);jp2.add(jbt[0]);jp2.add(jbt[21]);
        jp2.add(jbt[20]);jp2.add(jbt[15]);jp2.add(jbt[19]);jp2.add(jbt[18]);
        jp2.add(jbt[17]);jp2.add(jbt[16]);
	jfm.add(jp1);jfm.add(jp2);jfm.add(jtf);
        for(int i=0;i<21;i++)
            jbt[i].addActionListener(new MyActListener());
        jbt[21].addActionListener(new MyAceListener());
	jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	jfm.setResizable(false);
	jfm.setVisible(true);	
	}
        class MyActListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
              String s1=((JButton)e.getSource()).getText();
              if(!s1.equals("Back")&&!s1.equals("C")){
                  if(s1.equals("+/-")||s1.equals("1/x")){
                  if(s1.equals("+/-")){
                      jtf.setText(s2+"(+/-)");
                      s2=jtf.getText();
                  }
                  if(s1.equals("1/x")){
                      jtf.setText(s2+"1/");
                      s2=jtf.getText();
                  }
              }
                  else{
                   if(jtf.getText().equals("NaN")||jtf.equals("Error"))
                       s2="";
                   jtf.setText(s2+s1);
                   s2=jtf.getText();
                  }
              }
              else if(s1.equals("Back")||s1.equals("C")){
                  if(s1.equals("C")){
                      s2="";
                      jtf.setText("0");
                  }
                  if(s1.equals("Back")){
                      String ptr=jtf.getText();
                      count=ptr.length();
                      if(count-1<=0){
                         s2="";jtf.setText("0");
                         count--;
                      }
                      else if(count-1>0){
                          if(ptr.charAt(count-1)=='t'){
                              count=count-4;
                              if(count<=0){
                                  s2="";jtf.setText("0");
                              }
                              else {jtf.setText(ptr.substring(0,count));
                              s2=jtf.getText();
                              }
                          }
                         else if(ptr.charAt(count-1)=='/'){
                             count=count-2;
                             if(count<=0){
                                 s2="";jtf.setText("0");
                             }
                             else{ jtf.setText(ptr.substring(0,count));
                             s2=jtf.getText();
                             }
                         }
                         else if(ptr.charAt(count-1)==')'){
                             count=count-5;
                             if(count<=0){
                                 s2="";jtf.setText("0");
                             }
                             else {jtf.setText(ptr.substring(0,count));
                             s2=jtf.getText();
                             }
                         }
                         else if(ptr.charAt(count-1)=='N'){
                             count=count-3;
                             if(count<=0){
                                 s2="";jtf.setText("0");
                             }
                             else{ jtf.setText(ptr.substring(0,count));
                                 s2=jtf.getText();
                             }
                         }
                         else if(ptr.charAt(count-1)=='r'){
                             count=count-5;
                             if(count<=0){
                                 s2="";jtf.setText("0");
                             }
                             else{jtf.setText(ptr.substring(0,count));
                               s2=jtf.getText();
                             }
                         }
                         else {jtf.setText(ptr.substring(0,--count));
                         s2=jtf.getText();
                         }
                      }
                     //
                  }
              } 
              //
     }         
 }
class MyAceListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
            str1=jtf.getText();
            SetCount=-1;
            for(int i=0;i<str1.length();i++){
                if(i+1<str1.length()&&str1.charAt(i)=='t'&&str1.charAt(i+1)=='-'){
                    jtf.setText("NaN");SetCount=1;
                }
                else if(i+1<str1.length()&&(str1.charAt(i)=='/'||str1.charAt(i)=='%')&&str1.charAt(i+1)=='0'){
                    jtf.setText("NaN");SetCount=1;
                }
            }
            int i=0;int getNum1=0;int getNum2=0;
            while(i<str1.length()&&SetCount==-1){
            if(i==0&&i<str1.length()&&str1.charAt(i)=='-'){
                i++;
                while(i<str1.length()&&str1.charAt(i)>='0'&&str1.charAt(i)<='9'){
                    i++;
                    if(i<str1.length()&&str1.charAt(i)=='.')
                        i++;
                }
                getNum1++;
            }
            else if(str1.charAt(i)>='0'&&str1.charAt(i)<='9'){
                while(i<str1.length()&&str1.charAt(i)>='0'&&str1.charAt(i)<='9'){
                    i++;
                    if(i<str1.length()&&str1.charAt(i)=='.')
                        i++;
                }
                getNum1++;
            }
            else if(str1.charAt(i)=='('){
                i+=5;
                if(str1.charAt(i)=='-')
                    i++;
                while(i<str1.length()&&str1.charAt(i)>='0'&&str1.charAt(i)<='9'){
                    i++;
                    if(i<str1.length()&&str1.charAt(i)=='.')
                        i++;
                }
                getNum1++;
            }
            else if(str1.charAt(i)=='s'){
                i+=4;
                while(i<str1.length()&&str1.charAt(i)>='0'&&str1.charAt(i)<='9'){
                    i++;
                if(i<str1.length()&&str1.charAt(i)=='.')
                    i++;
                }
                getNum1++;
            }
            else{
              i++;getNum2++;
            }
        }   
            if(SetCount==-1&&(getNum2+1)==getNum1){
            jtf.setText(a.getNbl(str1));
            s2=jtf.getText();
            }
            else if((getNum2+1)!=getNum1){
                jtf.setText("Error");
            }
            }
        }
public static void main(String[] args){
	calculator ca=new calculator();
}
}
